package edu.uoc.tdp.pac3.server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.*;
import javax.swing.JOptionPane;

import edu.uoc.tdp.pac3.common.*;
import edu.uoc.tdp.pac3.beans.*;
import edu.uoc.tdp.pac3.common.tools.*;

/**
 * Clase GestorInscripcionsImpl
 * 
 * @author Eloi Rebollo i Francesc Cabrera
 * @version 1.0
 */
public class GestorInscripcionsImpl extends UnicastRemoteObject implements GestorIncidenciesInterface {

    GestorDisc gestorDisc = null;
    TDSLanguageUtils mess = null;
    private static final String DATE = "(dd/mm/yyyy)";

    public GestorInscripcionsImpl() throws RemoteException {
        super();
        try {
            gestorDisc = new GestorDisc();
            gestorDisc.initConnection();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("con.db.err.open") + "\n" + e.getMessage(),
                    mess.getMessage("con.db.err"), JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * @return Una llista de tots les espais actius de l'empresa
     * @throws RemoteException
     */
    public Vector<ComboItem> vctEspais() throws RemoteException {

        List<Area> lstEspai = new ArrayList<Area>();
        Vector<ComboItem> vtcEspai = new Vector<ComboItem>();
        try {
            GestorEspais gEspais = new GestorEspais(gestorDisc);
            lstEspai = gEspais.getSpacesList();

            for (Iterator<Area> it = lstEspai.iterator(); it.hasNext();) {
                Area aux = it.next();
                ComboItem cmbItem = new ComboItem(aux.getIdArea(), "(" + aux.getIdArea() + ") "
                        + aux.getNameArea());
                vtcEspai.add(cmbItem);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("services.err.query.spacelist") + "\n" + e.getMessage(),
                    mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
        }
        return vtcEspai;
    }

    /**
     * @return Una llista de tots els usuaris actius de l'empresa
     * @throws RemoteException
     */
    public Vector<ComboItem> vctUsers() throws RemoteException {

        List<User> lstUser = new ArrayList<User>();
        Vector<ComboItem> vtcUser = new Vector<ComboItem>();
        try {
            GestorUsuaris gUsers = new GestorUsuaris(gestorDisc);
            lstUser = gUsers.getUsersList();

            for (Iterator<User> it = lstUser.iterator(); it.hasNext();) {
                User aux = it.next();
                ComboItem cmbItem = new ComboItem(aux.getIdUser(), "(" + aux.getIdUser() + ") "
                        + aux.getNameUser());
                vtcUser.add(cmbItem);
            }
        } catch (Exception eE) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("services.err.query.userlist") + "\n" + eE.getMessage(),
                    mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
        }
        return vtcUser;
    }

    /**
     * @return Una llista de totes les incidencies de l'empresa
     * @throws RemoteException
     */
    public Vector<ComboItem> vctIncidencia() throws RemoteException {

        List<Incidencia> lstIncidencia = new ArrayList<Incidencia>();
        Vector<ComboItem> vctIncidencia = new Vector<ComboItem>();
        //SimpleDateFormat sdf = new SimpleDateFormat(DATE);
        String txt = new String(mess.getMessage("action.combo.incidencia.name"));
        try {
            GestorIncidencies gIncidencia = new GestorIncidencies(gestorDisc);
            lstIncidencia = gIncidencia.getRequestsList();

            for (Iterator<Incidencia> it = lstIncidencia.iterator(); it.hasNext();) {
                Incidencia aux = it.next();

                Date d = new Date();
                ComboItem cmbItem = new ComboItem(aux.getIdRequest(), txt + " # " + aux.getIdRequest()
                        + "(" + aux.getCreationDate() + ")");

                vctIncidencia.add(cmbItem);

            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("services.err.query.request") + "\n" + e.getMessage(),
                    mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
        }
        return vctIncidencia;
    }

    /**
     * @return Una llista de totes les incidencies de l'empresa segons el parametre
     * @param solved
     * @throws RemoteException
     */
    public Vector<ComboItem> vctIncidencia(Boolean solved) throws RemoteException {

        List<Incidencia> lstIncidencia = new ArrayList<Incidencia>();
        Vector<ComboItem> vctIncidencia = new Vector<ComboItem>();
        //SimpleDateFormat sdf = new SimpleDateFormat(DATE);
        String txt = new String(mess.getMessage("action.combo.incidencia.name"));
        try {
            GestorIncidencies gIncidencia = new GestorIncidencies(gestorDisc);
            lstIncidencia = gIncidencia.getRequestsList(solved);

            for (Iterator<Incidencia> it = lstIncidencia.iterator(); it.hasNext();) {
                Incidencia aux = it.next();

                Date d = new Date();
                ComboItem cmbItem = new ComboItem(aux.getIdRequest(), txt + " # " + aux.getIdRequest()
                        + "(" + aux.getCreationDate() + ")");

                vctIncidencia.add(cmbItem);

            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("services.err.query.request") + "\n" + e.getMessage(),
                    mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
        }
        return vctIncidencia;
    }

    /**
     * Guardar la incidencia
     * @param IdEspai, IdUser, Data, Descripcio
     * @return id del nou registre
     * @throws RemoteException
     */
    public Integer saveRequest(Integer idSpace, Integer idUser, Date fecha,
            String description) throws RemoteException {
        Integer ret = -1;
        GestorIncidencies gRequest = new GestorIncidencies(gestorDisc);
        try {
            gRequest.getConnection().setAutoCommit(false);
            ret = gRequest.addRequest(new Incidencia(idSpace, idUser, fecha, description));
            gRequest.getConnection().commit();
            gRequest.getConnection().setAutoCommit(true);
        } catch (SQLException e) {
            try {
                gRequest.getConnection().rollback();
                gRequest.getConnection().setAutoCommit(true);
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("services.err.query.unknown") + "\n" + e1.getMessage(),
                        mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
            }
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("services.err.query.sql") + "\n" + e.getMessage(),
                    mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
        }
        return ret;
    }

    /**
     * Actualitza la incidencia
     * @param IdIncidencia, Solved
     * @return id del registre modificat
     * @throws RemoteException
     */
    public Integer updateRequest(Integer idRequest, Boolean solved) throws RemoteException {

        Integer ret = -1;
        GestorIncidencies gRequest = new GestorIncidencies(gestorDisc);
        try {
            gRequest.getConnection().setAutoCommit(false);
            ret = gRequest.updateRequest(idRequest, solved);
            gRequest.getConnection().commit();
            gRequest.getConnection().setAutoCommit(true);
        } catch (SQLException e) {
            try {
                gRequest.getConnection().rollback();
                gRequest.getConnection().setAutoCommit(true);
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("services.err.query.unknown") + "\n" + e1.getMessage(),
                        mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
            }
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("services.err.query.sql") + "\n" + e.getMessage(),
                    mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
        }
        return ret;
    }

    /**
     * Guardar una actuació
     * @param IdRequest, IdUser, Data, Descripcio
     * @return id del nou registre
     * @throws RemoteException
     */
    public Integer saveNote(Integer idRequest, Integer idUser, Date fecha,
            String description) throws RemoteException {
        Integer ret = -1;
        GestorActuacions gNote = new GestorActuacions(gestorDisc);
        try {
            gNote.getConnection().setAutoCommit(false);

            ret = gNote.addNote(new Actuacio(idRequest, idUser, fecha, description));
            gNote.getConnection().commit();
            gNote.getConnection().setAutoCommit(true);
        } catch (SQLException e) {
            try {
                gNote.getConnection().rollback();
                gNote.getConnection().setAutoCommit(true);
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("services.err.query.unknown") + "\n" + e1.getMessage(),
                        mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
            }
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("services.err.query.sql") + "\n" + e.getMessage(),
                    mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
        }
        return ret;
    }

    /**
     * @return Una llista de totes les actuacions d' una
     * @param IDRequest
     * @throws RemoteException
     */
    public List<Actuacio> lstNoteList(Integer idRequest) throws RemoteException {

        List<Actuacio> lstNote = new ArrayList<Actuacio>();
        try {
            GestorActuacions gActuacio = new GestorActuacions(gestorDisc);
            lstNote = gActuacio.getNoteList(idRequest);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("services.err.query.request") + "\n" + e.getMessage(),
                    mess.getMessage("services.err.server"), JOptionPane.ERROR_MESSAGE);
        }
        return lstNote;
    }
}
